package com.mark.utils;

import java.math.BigDecimal;
import java.util.Map;

/**
 * Created by Choisaaaa on 2018/6/22.
 * 计算商品价格的工具类
 */
public class BigDecimalUtil {
    private BigDecimalUtil(){}//声明私有构造器，不让该类产生实例public static BigDecimal add(double v1,double v2)

    //加法运算
    public static BigDecimal add(double v1,double v2){
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return  b1.add(b2);
    }

    //减法运算
    public static BigDecimal sub(double v1,double v2){
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return  b1.subtract(b2);
    }

    //乘法运算
    public static BigDecimal mul(double v1,double v2){
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return  b1.multiply(b2);
    }

    //除法运算
    public static BigDecimal div(double v1,double v2){
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//四舍五入,保留2位小数
    }

    //比较两个金额的大小
    public static Boolean equals(Double d1,Double d2){
        Double result = Math.abs(d1-d2);
        if (result < 0.01) {
            return true;
        }else{
            return false;
        }
    }

}
